const fetchUserLogin = require('../api/fetchLogin')
const jwt = require('../utils/jwt')
const userModel = require('../model/user.js')
module.exports = class {
  static async login(ctx) {
    const {code} = ctx.request.body
    const res = await fetchUserLogin(code)
    // console.log(res.data);
    jwt.createToken(res.data)
    const data = {
      openid: res.data.openid,
      session_key: res.data.session_key
    }
    const findRes = await userModel.find({openid: res.data.openid})
    !findRes.length && await userModel.insertMany([data])
    ctx.body = {
      err: 0,
      msg: 'success',
      data: {token: res.data.openid}
    }
  }
}